package com.siyu.rehearsal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.basedata.xajresult.domain.XajResult;
import com.siyu.common.core.controller.BaseController;
import com.siyu.common.core.domain.AjaxPageResult;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.rehearsal.previewnd.domain.PreviewNd;
import com.siyu.rehearsal.previewresult.domain.PreviewResult;
import com.siyu.rehearsal.previewriska.domain.PreviewRiskA;
import com.siyu.rehearsal.previewriska.service.IPreviewRiskAService;
import com.siyu.rehearsal.previewsresult.domain.PreviewSresult;
import com.siyu.rehearsal.service.PlanHomepageService;
import com.siyu.rehearsal.vo.PlanReservoirVo;
import com.siyu.rehearsal.vo.PlanXajResultVo;
import com.siyu.rehearsal.vo.PptnRResultVo;
import com.siyu.rehearsal.vo.PreviewNdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/rehearsal/homepage")
public class PlanHomepageController extends BaseController {

    @Autowired
    private PlanHomepageService planHomepageService;

    @Autowired
    private IPreviewRiskAService previewRiskAService;

    @Autowired
    private IPlanService planService;


    /**
     * 可视化风险评估
     * @param id
     * @return
     */
    @GetMapping("/plan/risk")
    public AjaxResult getPreviewRiskA(@RequestParam("id")String id){
        QueryWrapper<PreviewRiskA> previewRiskAQueryWrapper = new QueryWrapper<>();
        previewRiskAQueryWrapper.select("affect", "d_populace", "i_area", "s_depth", "d_houses", "d_farmland","sm_depth")
                .eq("plan_id", id);
        PreviewRiskA previewRiskA = previewRiskAService.getOne(previewRiskAQueryWrapper);
        return AjaxResult.success(previewRiskA);
    }

    /**
     * 村庄风险评估
     * @param id
     * @return
     */
    @GetMapping("/plan/villagerisk")
    public AjaxResult getPreviewAffectV(@RequestParam("id")String id,
                                         @RequestParam("pageSize")Integer pageSize,
                                         @RequestParam("pageNum")Integer pageNum){
        AjaxPageResult ajaxPageResult = planHomepageService.getPreviewAffectV(id, pageSize, pageNum);
        return AjaxResult.success(ajaxPageResult);
    }

    /**
     * 水库实时指标序列查询
     * @param id
     * @return
     */
    @GetMapping("/plan/reservoir")
    public AjaxResult getReservoir(@RequestParam("id") String id) {
        try {
            Object previewResults = planHomepageService.getReservoir(id);
            return AjaxResult.success(previewResults);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("数据查询失败!");
        }
    }


    /**
     * 预演方案详情查询
     * @param id
     * @return
     */
    @GetMapping("/plan/particulars")
    public AjaxResult getParticulars(@RequestParam("id") String id) {
        QueryWrapper<Plan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.select("plan_name", "start_time", "end_time", "total_rainfall", "risk_danger", "scene_type","create_time","plan_desc")
                .eq("id", id);
        Plan plan = planService.getOne(planQueryWrapper);
        // 格式化total_rainfall，保留一位小数
        if (plan != null && plan.getTotalRainfall() != null) {
            DecimalFormat df = new DecimalFormat("#.0");
            String formattedTotalRainfall = df.format(plan.getTotalRainfall());
            plan.setTotalRainfall(Double.parseDouble(formattedTotalRainfall));
        }
        return AjaxResult.success(plan);
    }

    /**
     * 预演方案面雨量详情查询
     * @param id
     * @return
     */
    @GetMapping("/plan/rainfall")
    public AjaxResult getRainfall(@RequestParam("id") String id) {
        ArrayList<PreviewNdVo> previewNdVos = planHomepageService.getRainfall(id);
        return AjaxResult.success(previewNdVos);
    }

    /**
     * 预演可视化地图geojson数据返回
     * @param id
     * @return
     */
    @GetMapping("/plan/view")
    public AjaxResult getView(@RequestParam("id") String id,@RequestParam("dataType")Integer dataType) {
        List<String> list = planHomepageService.getView(id,dataType);
        return AjaxResult.success(list);
    }



}
